const cheerio = require("cheerio");
const babel = require("@babel/core");
const DefaultData = require("./DefaultData.js");

class UtilsIndex {
  static getHtmlLoad(pageCode) {
    return cheerio.load(pageCode, {
      xmlMode: true,
      decodeEntities: false
    })
  }

  static getScriptAst(scriptCode, plugins = []) {
    return babel.parse(scriptCode, {
      sourceType: 'module',
      configFile: false,
      plugins: plugins || DefaultData.jsAstPlugins
    }) || {}
  }

  static getAstSyncCode(scriptAst, plugins = []) {
    const {code: changeScript} = babel.transformFromAstSync(scriptAst, null, {
      configFile: false,
      plugins: plugins || DefaultData.jsAstPlugins
    });
    return changeScript
  }
}

module.exports = UtilsIndex
